package dom4j;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.Iterator;
import java.util.List;

public class TestDom4jParser {

    public static void main(String[] args) {


        //创建读取xml的工具
        SAXReader reader=new SAXReader();

        try {
            //读取xml文件，并解析，编程一棵树（dom4j自己做的树）
            Document document=reader.read(TestDom4jParser.class
                                                         .getClassLoader()
                                                         .getResourceAsStream("students.xml"));

            Element classElement=document.getRootElement();

            System.out.println(classElement);

            System.out.println(classElement.getName());

            String no=classElement.attributeValue("no");

            System.out.println(no);

            List<Element> studentElements=classElement.elements("student");
//            Iterator<Element> studentIterator=classElement.elementIterator("student");


            for (Element studentElement : studentElements) {
                List<Attribute> attributes=studentElement.attributes();

                for (Attribute attribute : attributes) {
                    System.out.println(attribute.getName()+"="+attribute.getValue());
                }

//                Element nameElement=studentElement.element("name");
//
//                System.out.println(nameElement.getText());

                System.out.println(studentElement.elementText("name"));
            }

        } catch (DocumentException e) {
            e.printStackTrace();
        }

    }

}
